package com.drug.service.impl;

import com.drug.domain.*;
import com.drug.mapper.DrugAtegoryMapper;
import com.drug.mapper.DrugCategoryMapper;
import com.drug.mapper.DrugInformationMapper;
import com.drug.service.DrugInformationSerice;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.taglibs.standard.tag.common.sql.DataSourceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.security.SecureRandom;
import java.util.List;
import java.util.Map;

@Service
public class DrugInformationSericeImpl<pri> implements DrugInformationSerice {

    @Autowired
    private DrugInformationMapper drugInformationMapper;

    @Autowired
    private DrugAtegoryMapper drugAtegoryMapper;

    @Autowired
    private DrugCategoryMapper drugCategoryMapper;

    /**
     * 查询全部药品品目
     * @return
     */
    @Override
    public Result fandAll() {
        List<DrugInformation> selectAll = drugInformationMapper.selectAll();
        Result result = new Result();
        result.setData(selectAll);
        return result;
    }

    @Override
    public Result removeBydiId(Integer diId) {
        int i = drugInformationMapper.deleteBydiId(diId);
        Result result = new Result();
        result.setCode(i);
        return result;
    }

    /**
     * 添加
     * @param map
     * @return
     */
    @Override
    public Result addDrugInformation(Map map) {
        Result result = new Result();

        String name = (String) map.get("diName");
        String diForm = (String) map.get("diForm");
        String diSpecifications = (String) map.get("diSpecifications");
        String diUnit = (String) map.get("diUnit");
        String diCc = (String) map.get("diCc");
        String diDaCategory = (String) map.get("diDaCategory");
        int i2 = Integer.parseInt(diDaCategory);
        String diState = (String) map.get("diState");
        Object daId =  map.get("daId");
        DrugInformation drugInformation = new DrugInformation();
        drugInformation.setDiName(name);
        drugInformation.setDiForm(diForm);
        drugInformation.setDiSpecifications(diSpecifications);
        drugInformation.setDiUnit(diUnit);
        drugInformation.setDiCc(diCc);
        drugInformation.setDiDaCategory(i2);
        drugInformation.setDiState(diState);
        drugInformation.setDiDaId((Integer) daId);
        int i = drugInformationMapper.insertDrugInformation(drugInformation);
        if (i == 1){
            result.setMsg("新增成功");
        }else {
            result.setMsg("新增失败");

        }

        return  result;
    }


    /**
     * 修改药品信息
     * @param drugInformation
     * @return
     */
    @Override
    public Result updateDrugInformation2(DrugInformation drugInformation) {
        /*int daId = drugAtegoryMapper.updateDrugAtegorydaName(drugAtegory);
        drugInformation.setDiDaId(daId);*/
        int i = drugInformationMapper.updateDrugInformation2(drugInformation);
        Result result = new Result();
        if(i == 1){
            result.setMsg("修改成功");
        }else{
            result.setMsg("修改失败");
        }
        return result;
    }
    @Override
    public Result insertDrugInformation2(DrugInformation drugInformation) {
        Result result = new Result();

        int i = drugInformationMapper.insertDrugInformation2(drugInformation);
        if (i == 1){
            result.setMsg("新增成功");
        }else {
            result.setMsg("新增失败");
        }
        /*String str= (String) map.get("diBiddingPrice");

        //BigDecimal bi=new BigDecimal((Integer) );
        String diNumer = (String) map.get("diNumer");
        String diName = (String) map.get("diName");
        String diForm = map.get("diForm").toString();
        String diSpecifications = (String) map.get("diSpecifications");
        String diUnit = (String) map.get("diUnit");
        String diCc = (String) map.get("diCc");
        String mf = (String) map.get("mfName");
        int mfName = Integer.parseInt(mf);
        BigDecimal diBiddingPrice = (BigDecimal) map.get("diBiddingPrice");
        String diApprovalNumber = (String) map.get("diApprovalNumber");
        String diApprovalTerm = (String) map.get("diApprovalTerm");
        String diDs = (String) map.get("diImportDrugs");
        int diImportDrugs = Integer.parseInt(diDs);
        String diMaterialQuality = (String) map.get("diMaterialQuality");
        String diBunit = (String) map.get("diBunit");
        BigDecimal diNewPrice = (BigDecimal) map.get("diNewPrice");
        BigDecimal diRetailPrice = (BigDecimal) map.get("diRetailPrice");
        String diDqlName = (String) map.get("diDqlName");
        String diDqlexplain = (String) map.get("diDqlexplain");
        String diInspectionReport = (String) map.get("diInspectionReport");
        String di = (String) map.get("diInspectionNumber");
        int diInspectionNumber = Integer.parseInt(di);
        String diInspectionTerm = (String) map.get("diInspectionTerm");
        String diDescription = (String) map.get("diDescription");
        String diTradingStatus = (String) map.get("diTradingStatus");
        Object daId =  map.get("daId");
        DrugInformation drugInformation = new DrugInformation();
        drugInformation.setDiNumer(diNumer);
        drugInformation.setDiName(diName);
        drugInformation.setDiForm(diForm);
        drugInformation.setDiSpecifications(diSpecifications);
        drugInformation.setDiUnit(diUnit);
        drugInformation.setDiCc(diCc);
        drugInformation.setDiMfId(mfName);
        //drugInformation.setDiBiddingPrice(diBiddingPrice);
        drugInformation.setDiApprovalNumber(diApprovalNumber);
        drugInformation.setDiApprovalTerm(diApprovalTerm);
        drugInformation.setDiImportDrugs(diImportDrugs);
        drugInformation.setDiMaterialQuality(diMaterialQuality);
        drugInformation.setDiBunit(diBunit);
        drugInformation.setDiNewPrice((BigDecimal) map.get("diNewPrice"));
        //drugInformation.setDiRetailPrice(diRetailPrice);
        drugInformation.setDiDqlName(diDqlName);
        drugInformation.setDiDqlexplain(diDqlexplain);
        drugInformation.setDiInspectionReport(diInspectionReport);
        drugInformation.setDiInspectionNumber(diInspectionNumber);
        drugInformation.setDiInspectionTerm(diInspectionTerm);
        drugInformation.setDiDescription(diDescription);
        drugInformation.setDiTradingStatus(diTradingStatus);
        drugInformation.setDiDaId((Integer) daId);
        int i = drugInformationMapper.insertDrugInformation2(drugInformation);
        if (i == 1){
            result.setMsg("新增成功");
        }else {
            result.setMsg("新增失败");
        }*/
        return result;
    }

    /**
     * 药品品目导出
     * 根据类别id查询名字
     * @param id
     * @return
     */
    @Override
    public String findNameById(Integer id) {
        return drugInformationMapper.selectNameById(id);
    }

    /**
     * 根据id回显
     * @param diId
     * @return
     */
    @Override
    public Result selectDiById(Integer diId) {
        Result result = new Result();
        //根据id查询药品信息(主键id)
        DrugInformation drugInformation = drugInformationMapper.selectDiById(diId);
/*        //根据id查询药品品目
        DrugAtegory drugAtegory = drugAtegoryMapper.selectDaById(drugInformation.getDiDaId());
        //根据id查询药品类别
        DrugCategory drugCategory = drugCategoryMapper.selectByPrimaryKey(drugInformation.getDiDaCategory());*/
        result.setData(drugInformation);
        return result;
    }

    /**
     * 根据id回显药品信息
     * @param diId
     * @return
     */
    @Override
    public Result selectDrugInformationById(Integer diId) {
        Result result = new Result();
        //根据id查询药品信息(主键id)
        DrugInformation drugInformation = drugInformationMapper.selectDrugInformationById(diId);
        result.setData(drugInformation);
        return result;
    }

    /**
     * 修改药品品目
     * @param
     * @return
     */
    @Override
    public Result updateDrugInformation(Map map) {
        Result result = new Result();
        String name = (String) map.get("diName");
        String daNumber = (String) map.get("daNumber");
        String diForm = (String) map.get("diForm");
        String diSpecifications = (String) map.get("diSpecifications");
        String diUnit = (String) map.get("diUnit");
        String diCc = (String) map.get("diCc");
        String diDaCategory = (String) map.get("diDaCategory");
        int i2 = Integer.parseInt(diDaCategory);
        String diState = (String) map.get("diState");
        String daId = (String) map.get("daId");
        String diId = (String) map.get("diId");
        DrugInformation drugInformation = new DrugInformation();
        drugInformation.setDiName(name);
        drugInformation.setDiForm(diForm);
        drugInformation.setDiSpecifications(diSpecifications);
        drugInformation.setDiUnit(diUnit);
        drugInformation.setDiCc(diCc);
        drugInformation.setDiDaCategory(i2);
        drugInformation.setDiState(diState);
        drugInformation.setDiDaId(Integer.parseInt(daId));
        drugInformation.setDiId(Integer.parseInt(diId));

        DrugAtegory drugAtegory = new DrugAtegory();
        drugAtegory.setDaId(Integer.parseInt(daId));
        Integer diDaId = drugInformation.getDiDaId();
        drugAtegory.setDaNumber(Integer.parseInt(daNumber));
        int i1 = drugAtegoryMapper.updateDrugAtegory(drugAtegory);
        int i = drugInformationMapper.updateDrugInformation(drugInformation);

        if (i+i1>=2){
            result.setMsg("修改成功");
        }else {
            result.setMsg("修改失败");
        }
        return result;
    }


    /**
     * 多条件查询
     * @param map
     * @param page
     * @return
     */
    @Override
    public Result findConditional(Map map, Integer page) {
        //使用分页插件
        PageHelper.startPage(page,5);
        Result result = new Result();
        //药品品目
        DrugAtegory drugAtegory = new DrugAtegory();
        //药品类别
        DrugCategory drugCategory = new DrugCategory();

        String number = (String) map.get("number");
        if (number==null||number.equals("")){
            number=null;
        }else {
            int daNumber = Integer.parseInt(number);
            drugAtegory.setDaNumber(daNumber);
        }
        String diName = (String) map.get("cname");
        if (diName==null||diName.equals("")){
            diName=null;
        }
        String diForm = (String) map.get("jixing");
        if (diForm==null||diForm.equals("")){
            diForm=null;
        }
        String diSpecifications = (String) map.get("guige");
        if (diSpecifications==null||diSpecifications.equals("")){
            diSpecifications=null;
        }
        String diUnit = (String) map.get("danwei");
        if (diUnit==null||diUnit.equals("")){
            diUnit=null;
        }
        String diCc = (String) map.get("cc");
        if (diCc==null||diCc.equals("")){
            diCc=null;
        }
        String diDaCategory = (String) map.get("leibie");
        if (diDaCategory==null||diDaCategory.equals("")){
            diDaCategory=null;
        }else {
            drugCategory.setDcName(diDaCategory);

        }
        String state = (String) map.get("zhuangtai");
        if (state==null||state.equals("")){
            state=null;
        }
        DrugInformation drugInformation = new DrugInformation();
        drugInformation.setDiForm(diForm);
        drugInformation.setDiSpecifications(diSpecifications);
        drugInformation.setDiUnit(diUnit);
        drugInformation.setDiCc(diCc);
        drugInformation.setDiState(state);
        drugInformation.setDiName(diName);
        drugInformation.setDiIsdel(0);
        drugInformation.setDrugAtegory(drugAtegory);
        drugInformation.setDrugCategory(drugCategory);
        //多条件查询
        List<DrugInformation> drugInformations = drugInformationMapper.selectConditional(drugInformation);
        PageInfo<DrugInformation> pageInfo= new PageInfo<DrugInformation>(drugInformations);
        result.setData(pageInfo);
        return result;
    }

    /**
     * 查询药品交易明细
     * @param map
     * @param page
     * @return
     */
    @Override
    public Result selectTransactionDetail(Map map, Integer page) {
        //使用分页插件
        PageHelper.startPage(page,5);
        Result result = new Result();

        //采购单
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        //医院
        Hospital hospital = new Hospital();
        //供货商
        Supplier supplier = new Supplier();
        //生产企业
        Manufacturer manufacturer = new Manufacturer();
        //药品品目
        DrugAtegory drugAtegory = new DrugAtegory();
        //质量层次
        DrugQualityLevel drugQualityLevel = new DrugQualityLevel();
        //药品类别
        DrugCategory drugCategory = new DrugCategory();
        //中间表
        PodiPd podiPd = new PodiPd();
        //药品信息
        DrugInformation drugInformation = new DrugInformation();



        String number = (String) map.get("poNumber");
        if (number==null||number.equals("")){
            number = null;
        }else{
            purchaseOrder.setPoNumber(number);
        }
        String poName = (String) map.get("poName");
        if (poName==null||poName.equals("")){
            poName = null;
        }else{
            purchaseOrder.setPoName(poName);
        }
        String poState = (String) map.get("poState");
        if (poState==null||poState.equals("")){
            poState = null;
        }else{
            purchaseOrder.setPoState(poState);
        }
        String hName = (String) map.get("hName");
        if (hName==null||hName.equals("")){
            hName = null;
        }else{
            hospital.sethName(hName);
        }
        String ghs = (String) map.get("ghs");
        if (ghs==null||ghs.equals("")){
            ghs = null;
        }else{
            supplier.setSurName(ghs);
        }
        String lefTime = (String) map.get("lefTime");
        if (lefTime==null||lefTime.equals("")){
            lefTime=null;
        }
        String rightTime = (String) map.get("rightTime");
        if (rightTime==null||rightTime.equals("")){
            rightTime=null;
        }
        String diNumer = (String) map.get("diNumer");
        if (diNumer==null||diNumer.equals("")){
            diNumer = null;
        }else{
            drugInformation.setDiNumer(diNumer);
        }
        String diName = (String) map.get("diName");
        if (diName==null||diName.equals("")){
            diName = null;
        }else{
            drugInformation.setDiName(diName);
        }
        String diForm = (String) map.get("diForm");
        if (diForm==null||diForm.equals("")){
            diForm = null;
        }else{
            drugInformation.setDiForm(diForm);
        }
        String diSpecifications = (String) map.get("diSpecifications");
        if (diSpecifications==null||diSpecifications.equals("")){
            diSpecifications = null;
        }else{
            drugInformation.setDiSpecifications(diSpecifications);
        }
        String diUnit = (String) map.get("diUnit");
        if (diUnit==null||diUnit.equals("")){
            diUnit = null;
        }else{
            drugInformation.setDiUnit(diUnit);
        }
        String diCc = (String) map.get("diCc");
        if (diCc==null||diCc.equals("")){
            diCc = null;
        }else{
            drugInformation.setDiCc(diCc);
        }
        String mfName = (String) map.get("mfName");
        if (mfName==null||mfName.equals("")){
            mfName = null;
        }else{
            manufacturer.setMfName(mfName);
        }
        String daName = (String) map.get("daName");
        if (daName==null||daName.equals("")){
            daName = null;
        }else{
            drugAtegory.setDaName(daName);
        }
        String zlcc = (String) map.get("zlcc");
        if (zlcc==null||zlcc.equals("")){
            zlcc = null;
        }else{
            drugQualityLevel.setDqlName(zlcc);
        }
        String yplb = (String) map.get("yplb");
        if (yplb==null||yplb.equals("")){
            yplb = null;
        }else{
            drugCategory.setDcName(yplb);
        }



        drugInformation.setPodiPd(podiPd);
        drugInformation.setHospital(hospital);
        drugInformation.setDrugAtegory(drugAtegory);
        drugInformation.setDrugCategory(drugCategory);
        drugInformation.setDrugQualityLevel(drugQualityLevel);
        drugInformation.setManufacturer(manufacturer);
        drugInformation.setSupplier(supplier);
        drugInformation.setPurchaseOrder(purchaseOrder);



        List<DrugInformation> drugInformations = drugInformationMapper.selectTransactionDetail(drugInformation, lefTime, rightTime);
        PageInfo<DrugInformation> pageInfo= new PageInfo<DrugInformation>(drugInformations);
        result.setData(pageInfo);
        return result;
    }

    /**
     * 查询用户名是否已存在
     * @param diName
     * @return
     */
    @Override
    public Result diName(String diName) {
        Result result = new Result();
        String[] diName1 = drugInformationMapper.diName();
        for(int i = 0; i<diName1.length;i++){
            if(diName1[i].equals(diName)){
                result.setMsg("通用名已存在");
                result.setCode(0);
                break;
            }else{
                result.setMsg("通用名可用");
                result.setCode(1);
            }
        }
        return result;
    }

    @Override
    public Result diNumer(String diNumer) {
        Result result = new Result();
        String[] diNumer1 = drugInformationMapper.diNumer();
        for (int i = 0; i < diNumer1.length; i++) {
            if (diNumer.equals(diNumer1[i])) {
                result.setMsg("流水号已存在");
                result.setCode(0);
                break;
            } else {
                result.setCode(1);
            }
        }
        return result;
    }
}